//
//  smcWindowFrame.m
//  juhu
//
//	Copyright (C) 2007 Christian Martick, http://juhu-mac.blogspot.com
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.

#import "smcWindowFrame.h"
#import "smcThemes.h"
#import "EtchedTextCell.h"

@implementation smcWindowFrame

- (void) dealloc {
	[progWidget release];
	[super dealloc];
}

- (id)initWithFrame:(NSRect)frame styleMask:(unsigned int)style owner:(id)owner
{
	if([super initWithFrame:frame styleMask:style owner:owner])
	{
		//make background Movable
		[[self window] setMovableByWindowBackground: YES];
	
		// Some of Apple's HUD windows have a slight shadow below the title
		EtchedTextCell *cell = [[EtchedTextCell alloc] initTextCell: @""];
		[cell setFont:[NSFont fontWithName:@"LucidaGrande" size:12.0]];
		//[cell setShadowColor:[NSColor colorWithCalibratedWhite:32/255.0 alpha:0.5]];
		titleCell = cell;
		
		// get the regular controls out of the picture
		[closeButton setHidden:YES];
		[minimizeButton setHidden:YES];
		[zoomButton setHidden:YES];
		
		// according to Andy Matuschak, the superclass doesn't do it's job here...
		[[self window] setShowsResizeIndicator:(style & NSResizableWindowMask)];
		
		// close button, in the style of Matt Gemmell's HUD Window implementation
		NSButton *closeWidget = [[NSButton alloc] initWithFrame:NSMakeRect(3.0, [self frame].size.height - 16.0, 
																		   13.0, 13.0)];
		[self addSubview:closeWidget];
		[closeWidget setButtonType:NSMomentaryChangeButton];
		[closeWidget setBordered:NO];
		[closeWidget setImage:[NSImage imageNamed: @"HUDCloseButton"]];
		[closeWidget setImagePosition:NSImageOnly];
		[closeWidget setTarget:[self window]];
		[closeWidget setFocusRingType:NSFocusRingTypeNone];
		[closeWidget setAction:@selector(orderOut:)];
		[closeWidget setAutoresizingMask:NSViewMinYMargin];
		[closeWidget setToolTip: @"Close Window"];
		[closeWidget release];
		
		//smc addition + button, juhu related Christian Martick 2007
		
		NSButton *plusWidget = [[NSButton alloc] initWithFrame:NSMakeRect(20.0, [self frame].size.height - 16.0, 
																		   13.0, 13.0)];
		[self addSubview:plusWidget];
		[plusWidget setButtonType:NSMomentaryChangeButton];
		[plusWidget setBordered:NO];
		[plusWidget setImage:[NSImage imageNamed:@"HUDPlusButton"]];
		[plusWidget setImagePosition:NSImageOnly];
		[plusWidget setTarget:[self window]];
		[plusWidget setFocusRingType:NSFocusRingTypeNone];
		[plusWidget setAction:@selector(zoom:)];
		[plusWidget setAutoresizingMask:NSViewMinYMargin];
		[plusWidget setToolTip: @"Zoom"];
		[plusWidget release];
		
		//refresh button
		NSButton *refreshWidget = [[NSButton alloc] initWithFrame:NSMakeRect([self frame].size.width -51, [self frame].size.height - 16.0, 
																		   13.0, 13.0)];
		[self addSubview:refreshWidget];
		[refreshWidget setButtonType:NSMomentaryChangeButton];
		[refreshWidget setBordered:NO];
		[refreshWidget setImage:[NSImage imageNamed:@"HUDRefreshButton"]];
		[refreshWidget setImagePosition:NSImageOnly];
		[refreshWidget setTarget:[self window]];
		[refreshWidget setFocusRingType:NSFocusRingTypeNone];
		[refreshWidget setAction:@selector(refreshClick:)];
		[refreshWidget setAutoresizingMask:NSViewMinYMargin | NSViewMinXMargin];
		[refreshWidget setToolTip: @"Update"];
		[refreshWidget release];
		
		//home button
		NSButton *homeWidget = [[NSButton alloc] initWithFrame:NSMakeRect([self frame].size.width -34, [self frame].size.height - 16.0, 
																		   13.0, 13.0)];
		[self addSubview:homeWidget];
		[homeWidget setButtonType:NSMomentaryChangeButton];
		[homeWidget setBordered:NO];
		[homeWidget setImage:[NSImage imageNamed:@"HUDHomeButton"]];
		[homeWidget setImagePosition:NSImageOnly];
		[homeWidget setTarget:[self window]];
		[homeWidget setFocusRingType:NSFocusRingTypeNone];
		[homeWidget setAction:@selector(homeClick:)];
		[homeWidget setAutoresizingMask:NSViewMinYMargin | NSViewMinXMargin];
		[homeWidget setToolTip: @"Open jaiku page"];
		[homeWidget release];

		//preferences button
		NSButton *prefWidget = [[NSButton alloc] initWithFrame:NSMakeRect([self frame].size.width - 17, [self frame].size.height - 16.0, 
																		   13.0, 13.0)];
		[self addSubview:prefWidget];
		[prefWidget setButtonType:NSMomentaryChangeButton];
		[prefWidget setBordered:NO];
		[prefWidget setImage:[NSImage imageNamed:@"HUDPrefButton"]];
		[prefWidget setImagePosition:NSImageOnly];
		[prefWidget setTarget:[self window]];
		[prefWidget setFocusRingType:NSFocusRingTypeNone];
		[prefWidget setAction:@selector(prefClick:)];
		[prefWidget setAutoresizingMask:NSViewMinYMargin | NSViewMinXMargin];
		[prefWidget setToolTip: @"Show Preferences"];
		[prefWidget release];
		
		//channel select button
//		NSButton *channelWidget = [[NSButton alloc] initWithFrame:NSMakeRect([self frame].size.width -17-17, [self frame].size.height - 16.0, 
//																		   13.0, 13.0)];
//		[self addSubview:channelWidget];
//		[channelWidget setButtonType:NSMomentaryChangeButton];
//		[channelWidget setBordered:NO];
//		[channelWidget setImage:[NSImage imageNamed:@"HUDChannelButton"]];
//		[channelWidget setImagePosition:NSImageOnly];
//		[channelWidget setTarget:[self window]];
//		[channelWidget setFocusRingType:NSFocusRingTypeNone];
//		[channelWidget setAction:@selector(prefClick:)];
//		[channelWidget setAutoresizingMask:NSViewMinYMargin | NSViewMinXMargin];
//		[channelWidget setToolTip: @"Select stream or channel"];
//		[channelWidget release];
		
		//progress indicator
		progWidget = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect([self frame].size.width - 68, [self frame].size.height - 16.0, 
																		   13.0, 13.0)];
		[progWidget setStyle: NSProgressIndicatorSpinningStyle];
		[progWidget setDisplayedWhenStopped: NO];
		[self addSubview: progWidget];
		[progWidget setAutoresizingMask:NSViewMinYMargin | NSViewMinXMargin];
		if ([[self window] respondsToSelector: @selector (__setWindowFrame:)])
			[[self window] __setWindowFrame: self];
		
		return self;
	}
	
	return nil;
}


+ (NSBezierPath*)_clippingPathForFrame:(NSRect)aRect
{
	float radius = 6.5;
	NSBezierPath *path = [NSBezierPath alloc];
	NSPoint topMid = NSMakePoint(NSMidX(aRect), NSMaxY(aRect));
	NSPoint topLeft = NSMakePoint(NSMinX(aRect), NSMaxY(aRect));
	NSPoint topRight = NSMakePoint(NSMaxX(aRect), NSMaxY(aRect));
	NSPoint bottomRight = NSMakePoint(NSMaxX(aRect), NSMinY(aRect));

	[path moveToPoint: topMid];
	[path appendBezierPathWithArcFromPoint: topRight
		toPoint: bottomRight
		radius: radius];
	[path appendBezierPathWithArcFromPoint: bottomRight
		toPoint: aRect.origin
		radius: radius];
	[path appendBezierPathWithArcFromPoint: aRect.origin
		toPoint: topLeft
		radius: radius];
	[path appendBezierPathWithArcFromPoint: topLeft
		toPoint: topRight
		radius: radius];
	[path closePath];
	
	return [path autorelease];
}

- (NSRect)_titlebarTitleRect
{
	NSRect rect = [super _titlebarTitleRect];
	rect.origin.y++;
	
	return rect;
}

- (void)_drawTitle:(NSRect)rect
{
	[self _drawTitleStringIn:rect withColor:[NSColor whiteColor]];
}

- (void)_drawTitleBar:(NSRect)rect
{
	//set colors depending on theme
	[[NSGraphicsContext currentContext] setPatternPhase: NSMakePoint(0,rect.origin.y-19)];
	[[smcThemes titleBarColor] set];
	NSRectFill(NSMakeRect(rect.origin.x,rect.origin.y,rect.size.width, rect.size.height));
	[[NSGraphicsContext currentContext] setPatternPhase: NSMakePoint(0,0)];

	[self _drawTitle:rect];
}

- (void)drawRect:(NSRect)_rect
{
	NSRect rect = [self frame];
	[[NSColor clearColor] set];
	
	NSRectFill(rect);
	NSRectFill(_rect);
	
	NSBezierPath *path = [[self class] _clippingPathForFrame: rect];
	[path addClip];
	
	[[smcThemes generalWindowColor] set];
	NSRectFill(rect);
	
	NSRect titleBarRect = rect;
	titleBarRect.origin.y += rect.size.height - [self titleBarHeight];
	titleBarRect.size.height = [self titleBarHeight];

	// draw footer
	[[smcThemes postFieldBackgroundColor] set];
	NSRect footer = rect;
	footer.size.height = 137;
	NSRectFill(footer);
	
	NSBezierPath *_path = [NSBezierPath bezierPath];
	[_path moveToPoint: NSMakePoint(rect.origin.x, footer.size.height-1)];
	[_path lineToPoint: NSMakePoint(rect.size.width, footer.size.height -1)];
	[[NSColor colorWithCalibratedRed: 0.224 green: 0.224 blue: 0.224 alpha:1] set];
	[_path setLineWidth: 0.5];
	[_path stroke];
	
	[self _drawTitleBar: titleBarRect];
}

- (NSRect)contentRectForFrameRect:(NSRect)frameRect styleMask:(unsigned int)aStyle
{
	frameRect.size.width -= 2;
	frameRect.origin.x += 1;
    frameRect.size.height -= [self titleBarHeight];
    return frameRect;
}


- (NSRect)frameRectForContentRect:(NSRect)windowContent styleMask:(unsigned int)aStyle
{
	windowContent.size.width += 2;
	windowContent.origin.x -= 1;
    windowContent.size.height += [self titleBarHeight];
    return windowContent;
}

- (NSSize)_topCornerSize
{
	return NSMakeSize(0, [self titleBarHeight]);
}

-(float)titleBarHeight
{
	return 19;
}

- (void)_drawResizeIndicators:(NSRect)rect
{
	if (![[self window] showsResizeIndicator])
		return;
	NSPoint resizeOrigin = NSMakePoint(NSMaxX([self frame]) - 3, 3);
	NSBezierPath *resizeGrip = [NSBezierPath bezierPath];
	[resizeGrip moveToPoint:NSMakePoint(resizeOrigin.x, resizeOrigin.y + 2)];
	[resizeGrip lineToPoint:NSMakePoint(resizeOrigin.x - 3, resizeOrigin.y)];
	[resizeGrip moveToPoint:NSMakePoint(resizeOrigin.x, resizeOrigin.y + 6)];
	[resizeGrip lineToPoint:NSMakePoint(resizeOrigin.x - 7, resizeOrigin.y)];
		
	[resizeGrip setLineWidth:1.0];
	
	[[NSColor whiteColor] set];
	[resizeGrip stroke];	
}

-(NSProgressIndicator *) progressIndicator
{
	//return spin wheel
	return progWidget;
}

@end
